import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:rainflutter/Tools/dcp_pch.dart';
import 'package:shimmer/shimmer.dart';

import '../twinkle_button/twinkle_button.dart';

///我的评论

class DcpMyComments extends StatefulWidget {
  @override
  _DcpMyCommentsState createState() => _DcpMyCommentsState();
}

class _DcpMyCommentsState extends State<DcpMyComments>
    with SingleTickerProviderStateMixin {
  List<String> addStrList = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"];
  List<String> strList = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"];

  AnimationController _controller;
  @override
  void initState() {
    super.initState();
    _controller = AnimationController(vsync: this);
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('评论'),
          centerTitle: true,
        ),
        body: new EasyRefresh(
          child: ListView.builder(
            itemCount: strList.length,
            itemBuilder: (context, index) {
              return new Column(
                children: <Widget>[
                  Container(child: commentsCell(context, index)),
                  Container(height: 1, color: Color(0xffebebeb))
                ],
              );
            },
          ),
          onRefresh: () async {
            await new Future.delayed(const Duration(seconds: 1), () {
              setState(() {
                strList.clear();
                strList.addAll(addStrList);
              });
            });
          },
          onLoad: () async {
            if (strList.length < 20) {
              await new Future.delayed(const Duration(seconds: 1), () {
                print('添加数据');
                setState(() {
                  strList.addAll(addStrList);
                });
              });
            } else {
              print('没数据了');
              // new Future.delayed(const Duration(milliseconds: 0), () {
              //   dcpAndroidRefreshFotter(_footerKeyList,context)
              //       .getKey()
              //       .currentState
              //       .onNoMore();
              // });
            }
          },
        ));
  }

  Widget commentsCell(BuildContext context, int index) {
    return Container(
        color: Colors.white,
        padding: EdgeInsets.all(12),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            //第一段公司名字 + 跳转详情
            Container(
              child: Container(
                color: Color(0xfff8f9f9),
                child: companyView(context, index),
              ),
            ),
            //第二段评价内容
            Container(
              padding: EdgeInsets.only(top: 12),
              child: Text(
                '轨迹飞行是全新的多点航线控制技术，只要在实时相机画面上划一道轨迹，飞行器就能将其转化为相应的航线，以当前高度沿指轨迹飞行是全新的多点航线控制技术，只要在实时相机画面上划一道轨迹，飞行器就能将其转化为相应的航线，以当前高度沿指应的航线，以当前高度沿指',
                style: TextStyle(
                  fontSize: 14.0,
                  color: Color(0xff333333),
                ),
                textAlign: TextAlign.left,
              ),
            ),
            //第三段评价内容 星星+时间+删除按钮
            Container(
              color: Colors.white,
              padding: EdgeInsets.only(top: 8, bottom: 0),
              child: bottomWidget(context, index),
            ),
            SizedBox(
              width: 200.0,
              height: 100.0,
              child: Shimmer.fromColors(
                  baseColor: Colors.red,
                  highlightColor: Colors.yellow,
                  child: Container(
                    // color: Colors.blue,
                    child: Text(
                      '直接购买',
                      textAlign: TextAlign.center,
                      style: TextStyle(
                        fontSize: 40.0,
                        color: Colors.white,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  )),
            ),
            SizedBox(
              width: 200.0,
              height: 50.0,
              child: Shimmer.fromColors(
                  baseColor: Colors.red,
                  highlightColor: Colors.yellow,
                  child: Container(
                    color: Colors.blue,
                    child: Text(
                      'Shimmer',
                      textAlign: TextAlign.center,
                      style: TextStyle(
                        fontSize: 40.0,
                        color: Colors.white,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  )),
            ),
            SizedBox(height: 30,),
            TwinkleButton(
                buttonTitle: Text(
                  'Subscribe with Free Trial',
                  style: TextStyle(
                    color: Colors.white,
                    fontWeight: FontWeight.w300,
                    fontSize: 17.0,
                  ),
                ),
                needBreath:true,
                buttonColor: Color(0xff3dce89),
                onclickButtonFunction: () {
                  print('hello');
                })
          ],
        ));
  }

  Widget companyView(BuildContext context, int index) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      //crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Container(
          padding: EdgeInsets.only(left: 13),
          height: 40,
        ),
        Expanded(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Container(
                alignment: Alignment.centerLeft,
                child: Text(
                  strList[index],
                  style: TextStyle(
                    fontSize: 14.0,
                    color: Color(0xff333333),
                    fontWeight: FontWeight.w500, //medium暂时使用这个
                  ),
                  textAlign: TextAlign.center,
                ),
              )
            ],
          ),
        ),
        SizedBox(
          width: 10.0,
        ),
        Column(
          children: <Widget>[
            Container(
              width: 30,
              alignment: Alignment.centerRight,
              child: IconButton(
                icon: Icon(Icons.web),
                onPressed: () {
                  print('下一页');
                },
              ),
            )
          ],
        ),
      ],
    );
  }

  Widget bottomWidget(BuildContext context, int index) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      // crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Container(
            padding: EdgeInsets.only(top: 5),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                raingBar(4.8),
                timeView('2019-09-12 16:00'),
              ],
            )),
        Expanded(
          child: Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: <Widget>[
              Container(
                width: 40,
                height: 26,
                child: FlatButton(
                  color: Colors.white,
                  padding: EdgeInsets.all(0),
                  child: Text(
                    '删除',
                    style: TextStyle(color: Color(0xffff3b00), fontSize: 10),
                  ),
                  onPressed: () {
                    return showDialog(
                        context: context,
                        barrierDismissible: false,
                        builder: (BuildContext context) {
                          return CupertinoAlertDialog(
                            title: Text(
                              '确定删除此条评论么?',
                              style: TextStyle(
                                  color: Color(0xff333333), fontSize: 14),
                            ),
                            // content: Text('确定删除么'),
                            actions: <Widget>[
                              new CupertinoButton(
                                  onPressed: () {
                                    Navigator.of(context).pop();
                                  },
                                  child: Text(
                                    '取消',
                                    style: TextStyle(fontSize: 14),
                                  )),
                              new CupertinoButton(
                                  onPressed: () {
                                    Navigator.of(context).pop();
                                    setState(() {
                                      strList.removeAt(index);
                                    });
                                  },
                                  child: Text(
                                    '确认',
                                    style: TextStyle(
                                        color: Color(0xffff3b00), fontSize: 14),
                                  )),
                            ],
                          );
                        });
                  },
                ),
                decoration: BoxDecoration(
                    color: Color(0xfff5f5f5),
                    borderRadius: BorderRadius.circular(2.0),
                    border: Border.all(color: Color(0xffff3b00), width: 1)),
              ),
            ],
          ),
        ),
      ],
    );
  }

  Widget raingBar(double value) {
    return RatingBar(
      clickable: false,
      size: 16,
      color: Color(0xffff3b00),
      padding: 4,
      value: value,
    );
  }

  Widget timeView(String time) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Container(
          padding: EdgeInsets.only(top: 3, left: 27),
          child: Text(
            time,
            style:
                TextStyle(color: Color.fromRGBO(0, 0, 0, 0.25), fontSize: 12),
          ),
        )
      ],
    );
  }
}
